package com.mhg.execise_json.service.impl;

import com.alibaba.fastjson.JSON;
import com.mhg.execise_json.model.RemoteModel;
import com.mhg.execise_json.service.MyService;
import com.mhg.execise_json.utils.HttpUtils;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Service
public class MyServiceImpl  implements MyService {


    @Override
    public RemoteModel getRemote(String url) {

        //发送get请求，并得到返回的json字符串
        String jsonResult = HttpUtils.sendGet(url);

        RemoteModel model = null;
        //解析，转为java对象
        if (jsonResult!=null && !"".equals(jsonResult)){
            model = JSON.parseObject(jsonResult,RemoteModel.class);
        }
        System.out.println("解析到的java对象"+ model.toString());

        //TODO 下一步：将数据存入数据库表中即可

        return model;
    }
}
